<!DOCTYPE html>
<html lang="en">
<head>
    <title>SSE接收端</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: 'Microsoft YaHei', Arial, sans-serif;
        }

        body {
            background-color: #f5f7fa;
            color: #333;
            padding: 20px;
            line-height: 1.6;
        }

        .container {
            max-width: 1200px;
            margin: 0 auto;
            background-color: #fff;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
            overflow: hidden;
        }

        header {
            background: linear-gradient(135deg, #4b6cb7, #182848);
            color: white;
            padding: 20px;
            text-align: center;
            position: relative;
        }

        h1 {
            font-size: 24px;
            margin-bottom: 5px;
        }

        .status-indicator {
            display: inline-block;
            width: 12px;
            height: 12px;
            background-color: #4CAF50;
            border-radius: 50%;
            margin-right: 8px;
            animation: blink 1.5s infinite;
        }

        @keyframes blink {
            0% { opacity: 0.4; }
            50% { opacity: 1; }
            100% { opacity: 0.4; }
        }

        .status-text {
            font-size: 14px;
            color: #e0e0e0;
            margin-top: 5px;
        }

        .log-container {
            padding: 20px;
            height: 70vh;
            overflow-y: auto;
            background-color: #fafafa;
            border: 1px solid #eee;
            border-radius: 4px;
            margin: 20px;
        }

        #messages {
            display: flex;
            flex-direction: column;
            gap: 8px;
        }

        #messages div {
            padding: 12px;
            background-color: #fff;
            border-left: 4px solid #4b6cb7;
            box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
            border-radius: 4px;
            font-family: 'Courier New', monospace;
            word-break: break-all;
            transition: all 0.3s ease;
        }

        #messages div:hover {
            transform: translateX(5px);
            background-color: #f0f4ff;
        }

        .new-message {
            animation: fadeIn 0.5s;
        }

        @keyframes fadeIn {
            from { opacity: 0; transform: translateY(10px); }
            to { opacity: 1; transform: translateY(0); }
        }

        .controls {
            display: flex;
            justify-content: space-between;
            padding: 0 20px 20px 20px;
        }

        button {
            background-color: #4b6cb7;
            color: white;
            border: none;
            padding: 8px 16px;
            border-radius: 4px;
            cursor: pointer;
            transition: background-color 0.3s;
        }

        button:hover {
            background-color: #3a5795;
        }

        footer {
            text-align: center;
            padding: 15px;
            color: #666;
            font-size: 12px;
            border-top: 1px solid #eee;
        }

        @media (max-width: 768px) {
            .container {
                margin: 0;
                border-radius: 0;
            }

            .log-container {
                height: 60vh;
                margin: 10px;
                padding: 10px;
            }

            h1 {
                font-size: 20px;
            }
        }
    </style>
    <script>
        let eventSource;
        let connected = false;


        function connectSSE() {
            if (connected) return;

            eventSource = new EventSource('/sse/connect/' + new Date().getTime().toString());

            eventSource.onopen = function() {
                connected = true;
                updateConnectionStatus(true);
            };

            eventSource.onmessage = function(event) {
                let message = document.createElement('div');
                message.textContent = event.data;
                message.classList.add('new-message');

                const messagesContainer = document.getElementById('messages');
                messagesContainer.appendChild(message);

                // 自动滚动到底部
                const logContainer = document.querySelector('.log-container');
                logContainer.scrollTop = logContainer.scrollHeight;

                // 限制显示的日志数量，防止内存占用过多
                if (messagesContainer.children.length > 1000) {
                    messagesContainer.removeChild(messagesContainer.firstChild);
                }
            };

            eventSource.onerror = function() {
                connected = false;
                updateConnectionStatus(false);

                // 尝试重新连接
                setTimeout(connectSSE, 5000);
            };
        }

        function disconnectSSE() {
            if (eventSource) {
                eventSource.close();
                connected = false;
                updateConnectionStatus(false);
            }
        }

        function clearLogs() {
            document.getElementById('messages').innerHTML = '';
        }

        function updateConnectionStatus(isConnected) {
            const statusIndicator = document.querySelector('.status-indicator');
            const statusText = document.querySelector('.status-text');

            if (isConnected) {
                statusIndicator.style.backgroundColor = '#4CAF50';
                statusText.textContent = '已连接 - 正在接收日志';
            } else {
                statusIndicator.style.backgroundColor = '#F44336';
                statusText.textContent = '未连接 - 请检查连接';
            }
        }

        // 更新时间的函数
        function updateDateTime() {
            const now = new Date();
            const year = now.getFullYear();
            const month = (now.getMonth() + 1).toString().padStart(2, '0');
            const day = now.getDate().toString().padStart(2, '0');
            const hours = now.getHours().toString().padStart(2, '0');
            const minutes = now.getMinutes().toString().padStart(2, '0');
            const seconds = now.getSeconds().toString().padStart(2, '0');
            document.getElementById('currentDateTime').textContent = `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
        }

        setInterval(updateDateTime, 1000);

        // 页面加载完成后自动连接
        window.onload = function() {
            connectSSE();
            updateDateTime();
            // 在页面关闭前断开连接
            window.addEventListener('beforeunload', function() {
                disconnectSSE();
            });
        };
    </script>
</head>
<body>
<div class="container">
    <header>
        <h1>SSE信息接收端</h1>
        <div class="status-text">
            <span class="status-indicator"></span>
            <span>正在连接...</span>
        </div>
    </header>

    <div class="log-container">
        <div id="messages"></div>
    </div>

    <div class="controls">
        <button onclick="clearLogs()">清除日志</button>
        <div>
            <button onclick="disconnectSSE()">断开连接</button>
            <button onclick="connectSSE()">重新连接</button>
        </div>
    </div>

    <footer>
        SSE信息查看 &copy; <span id="currentDateTime"></span>
    </footer>
</div>
</body>
</html>